﻿var NDrag = {
    Data:{"0":null},
    State:0,
    DragState:0,
    ContainerPos:{},
    PosData:{},
    Draging:false,
    DragElement:0,
    TargetElement:null,
    TempElement:null,
    tabs:[],
    pages: [],
    selectedTab:null,//当前选中的tab
    init:function(categoryid){
        this.ContainerPos=$('#privateEntries' + categoryid).position();
        $("#dragItem").easydrag(true);
        $("#dragItem").ondrop(function(e,element){
            if(NDrag.ActionName){
                NDrag.cmd(NDrag.ActionName);
            }else{
                NDrag._drag(element);
            }
            setTimeout(function(){$('#search-box').show();},300);
            return false;
        }).mouseover(function(e){
                if($.browser.msie)window.status=NDrag.Data[NDrag.DragElement][2];
                return false;
        }).mouseout(function(e){
                NDrag.mouseOutBox();
                return false;
        }).ondrag(function(e){
                NDrag.Draging=true;
                NDrag._getTarget(e,categoryid);
                return false;
        });
        $("#privateEntries" + categoryid + " .span-cell").mouseover(function(e){
                NDrag.mouseOverBox(this,categoryid);
                return false;
            });
        for(var i=0;i< $("#privateEntries" + categoryid).children().length;i++)
        {
            var item = $("#privateEntries" + categoryid).children()[i];
            
            NDrag.Data[i] = $($("#privateEntries" + categoryid).children()[i]);
            var pos=$(item).position();
            NDrag.PosData[i]=[pos.left,pos.top,pos.left+80,pos.top+90];
        }
        $(window).unbind('resize').resize(function(){
            NDrag.resize();
        });
    },
    
    resize:function(){
        var temp=NDrag.ContainerPos;
        NDrag.ContainerPos=$('#privateEntries' + NDrag.selectedTab.elm.id.substring(4)).position();
        if(NDrag.ContainerPos.left!=temp.left){
            $('#dragItem').hide();
            $.each(NDrag.PosData,function(index,p){
                NDrag.PosData[index][0]=p[0]-(temp.left-NDrag.ContainerPos.left);
                NDrag.PosData[index][3]=p[3]-(temp.left-NDrag.ContainerPos.left);
            });
        }
        
        rukou.footerPosition();
    },
    
    initCategories:function(){
        var tabs = document.getElementById("categories").childNodes;
        for (var i = 0; i < tabs.length; i++) {
            if (tabs[i].nodeName.toLowerCase() == "a" && tabs[i].id != "addCategory")
                NDrag.tabs.push(new tab(tabs[i]));
        } //初始化所有的tab页标签

        var pages = document.getElementById("entries");
        for (var i = 0; i < pages.childNodes.length; i++) {
            if (pages.childNodes[i].nodeName.toLowerCase() == "div" && pages.childNodes[i].id.match(/^privateEntries/)) {
                var container = pages.childNodes[i];
                container.cells = [];
                NDrag.pages.push(container);
            }
        } //找到所有的页

        for (var i = 0; i < NDrag.tabs.length; i++) {
            if (NDrag.pages[i]) {
                var page = NDrag.pages[i]; //page
                NDrag.tabs[i].page = page;
                page.tab = NDrag.tabs[i]; //page和tab可以互相引用

            }
        }
        NDrag.tabs[0].select();
         
    },
    
    exchange:function(sourceId,targetId){
    
        NDrag.DragElement=NDrag.TargetElement;

        var sourceElement=NDrag.Data[sourceId];
        var targetElement=NDrag.Data[targetId];
        targetElement.removeClass('target');
        var _html=targetElement.html();
        targetElement.html(sourceElement.html());
        sourceElement.html(_html);
        
        
        var _itemId = targetElement.attr('id');
        targetElement.attr('id',sourceElement.attr('id'));
        sourceElement.attr('id',_itemId);
        
        
        $("#dragItem").html('');
        $("#dragItem").hide();
        
        $("#orderChanged").attr("value","yes");
    },
    
    _drag:function(e){
        var id=NDrag.TargetElement;

        $('#dragItem').animate({
            left:NDrag.PosData[id][0],
            top:NDrag.PosData[id][1]
            },200,function(){
                NDrag.Draging=false;
                if(NDrag.TargetElement!=NDrag.DragElement){
                    
                    NDrag.exchange(NDrag.DragElement,NDrag.TargetElement);
                }else{
                    $('#'+id).removeClass('draging');
                }
         NDrag._dragShowAction();
        });
    },
    mouseOverBox:function(e,categoryid){
        if(this.Draging)return;
        if(!this.MouseOut)this.mouseOutBox();
        this.MouseOut=false;
        
        for(var i=0;i< $("#privateEntries" + categoryid).children().length;i++)
        {
            var item = $("#privateEntries" + categoryid).children()[i];
            if(item.id == e.id)
            {
                var id=i;
                break;
            }
         }
         
         if($("#otherview").attr("value")=="False")
        {
             $("#dragItem").find("#set1").remove();
            $(this.Data[id]).find("#set").remove();
        
            //当鼠标放入入口时，显示修改和删除的按钮
            var setting = "<span id=\"set\" style=\"z-index:10;\"><a href=\"#\" onclick=\"rukou.rukouModify(" +e.id + ");return false;\"  id=\"modify\"><img  src=\"Image/modifypage.gif\" alt=\"修改入口\" title=\"修改入口\"  /></a>";
            setting += "  <a href=\"#\" id=\"delete\"  onclick=\"rukou.rukouDelete("+ categoryid + ");return false;\" ><img src=\"Image/Delete21.gif\"  alt=\"删除入口\" title=\"删除入口\"  /></a>";
            setting += "  <a href=\"#\" id=\"changepic\"  onclick=\"rukou.rukouChangePic(" + e.id + ",2);return false;\" ><img src=\"Image/changepic.gif\"  alt=\"修改入口背景图\" title=\"修改入口背景图\"  /></a></span>";
            $("#set").remove();
            $($("#" + e.id).children()[1]).append(setting);
            $("#delete").attr("name",e.id);
            $("#modify").attr("name", e.id);
        }
        
         this.DragElement=this.TempElement=this.TargetElement=id;
         var dragHTML=$(this.Data[id]).html().replace(/set/gim,"set1");
         $("#dragItem").html(dragHTML).css({left:this.PosData[id][0],top:this.PosData[id][1]}).show();
        
        
        //绑定鼠标点击入口时的事件
        $("#dragItem > div .t").click(function(e){
             NDrag.clickBox(NDrag.DragElement);
             return false;
        });
        
       
    },
    mouseOutBox:function(e){
        if(this.Draging)return;
        $("#dragItem").hide();
        if($.browser.msie)window.status='';
        $(NDrag.Data[this.DragElement]).removeClass('draging');
        this.MouseOut=true;
    },
    clickBox:function(id){
        if(this.Draging)return;
        var val = $(NDrag.Data[id].children()[0]).html().split('|');
        var url=val[0].replace("&amp;","&");
        window.open(url,'');
        $.ajax({
                type: "POST",
                url: "/Ajax/SetClickCount",
                data: { entryid: val[1] },
                success: function(data, textStatus) {
                }
        });
    },
    dragOverBox:function(id){
        $('#'+id).addClass('dragOver');
    },
    
    _dragAction:function(action){
        if(action==this.ActionName)return;
        $('#'+this.TempElement).removeClass('dragOver');
        this.TempElement=null;
        this.ActionName=action;
        $('#'+action).addClass('hover');
        },
        cmd:function(action){
        var id=this.DragElement;
        if(this.State==0){
        $('#loginDialog').click();
        }else{
        this[action.split('-')[1]+'Site'](id);
        }
        this.ShowAction=false;
        this.ActionName=null;
        setTimeout(function(){
        $('#'+action).removeClass('hover');
        $("#dragItem").hide();
        $('#'+(NDrag.State==2?'opt-mine':'opt-common')).hide();
        NDrag.Draging=false;
        },action=='mine-del'?0:500);
    },
    _dragShowAction:function(){
    },
    _getTarget:function(e,categoryid){

        var pos=$.getMousePosition(e);
        for(var k=0;k< $("#privateEntries" + categoryid).children().length;k++)
        {
            var item1 = $("#privateEntries" + categoryid).children()[k];
            var posItem=$(item1).position();
            
             if (pos.y <= posItem.top + 160 && pos.x <= posItem.left + 240 && pos.y >= posItem.top && pos.x >=posItem.left ) {
                NDrag.TargetElement =k;
                break;
            }
        }
        
        //NDrag.TargetElement=id;
        if(NDrag.TargetElement==this.TempElement)return;
        this.dragOverBox(NDrag.TargetElement);
        this.TempElement=NDrag.TargetElement;
    },
    _getTargetId:function(x,y){
        var _x=x%90;
        var _y=y%90;
        var xn=Math.ceil(x/90);
        var yn=Math.ceil(y/90);
        if(yn>=3&&yn<=4&&xn>=4&&xn<=7){
        if(y<90*2+10||x>90*7)return null;
        if(this.State==2){
        if(x<=90*3+116)return'mine-edit';
        if(x<=90*3+116+118)return'mine-del';
            return'mine-copy';
        }else{
            return'common-copy';
        }
        }
        if(_x<10||_y>80)return null;
        if(yn<=2)return 10*(yn-1)+xn;
        if(yn>=5)return 32+10*(yn-5)+xn;
        if(xn<=3)return 20+3*(yn-3)+xn;
        if(xn>=8)return 26+3*(yn-3)+(xn-7);
        return null;
    }
    
};